package com.itdaan.portal.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.itdaan.portal.business.mapper.CommonMapper;
import com.itdaan.portal.business.service.CommonService;
import com.itdaan.portal.common.exception.ServiceException;
/**
 * @author www.itdaan.com
 */
@Service(value = "commonService")
public class CommonServiceImpl implements CommonService {

	@Resource(name = "commonMapper")
	private CommonMapper commonMapper;
	
	@Override
	public String findLastTableName(String tableNamePre)
			throws ServiceException {
		return commonMapper.selectLastTableName(tableNamePre);
	}

	@Override
	public List<Map<String,Object>> findTableNames(Map<String, Object> condition)
			throws ServiceException {
		return commonMapper.selectTableNames(condition);
	}

	@Override
	public String findColumns(String tableName) throws ServiceException {
		return commonMapper.selectColumns(tableName);
	}

	@Override
	public void createPostTable(String fields, String tableName,
			long autoIncrement) throws ServiceException {
		StringBuffer sql = new StringBuffer("CREATE TABLE `");
		sql.append(tableName);
		sql.append("` (");
		sql.append("`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'id',");
		String fieldss [] = fields.split(",");
		for(String field : fieldss){
			if(field.split("#").length == 1){
				sql.append("`").append(field).append("`").append(" varchar(1000) DEFAULT NULL,");
			}else if(field.split("#").length == 2){
				sql.append("`").append(field.split("#")[0]).append("`").append(" longtext DEFAULT NULL,");
			}
		}
		//来源网址
		sql.append("`url` varchar(100) DEFAULT NULL COMMENT '来源网址',");
		//分类ID
		sql.append("`group_ids` varchar(100) DEFAULT NULL COMMENT '分类ID',");
		//网站ID
		sql.append("`web_id` varchar(100) DEFAULT NULL COMMENT '网站ID',");
		//网站类型ID
		sql.append("`type_id` varchar(100) DEFAULT NULL COMMENT '网站类型ID',");
		//查看次数
		sql.append("`view_num` int(11) default 0 COMMENT '查看次数',");
		//是否已经生成静态
		sql.append("`is_html_created` int(11) default 0 COMMENT '是否已经创建html静态 0 否 1是',");
		//静态路径
		sql.append("`html_path` varchar(100) default NULL COMMENT 'html创建路径',");
		//是否创建索引
		sql.append("`is_indexed` int(11) default 0 COMMENT '是否已经创建索引  0 否 1是',");
		//评论次数
		sql.append("`comment_num` int(11) default 0 COMMENT '评论次数',");
		//投票次数
		sql.append("`vote_num` int(11) DEFAULT '100' COMMENT '投票次数',");
		//创建时间
		sql.append("  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',");
		sql.append(" PRIMARY KEY (`id`),");
		sql.append(" KEY `index_type_id` (`type_id`) USING BTREE,");
		sql.append(" KEY `index_is_indexed` (`is_indexed`) USING BTREE,");
		sql.append(" KEY `index_is_html_created` (`is_html_created`) USING BTREE )");
		sql.append(" ENGINE=MyISAM AUTO_INCREMENT=");
		sql.append(autoIncrement);
		sql.append(" DEFAULT CHARSET=utf8 COMMENT='内容信息表' ");
		commonMapper.excuteDDL(sql.toString());
	}

	@Override
	public void createNextPostTable(String webType) throws ServiceException {

	}

	@Override
	public void dropPostTable(String tableNamePre) throws ServiceException {
		Map<String,Object> condition = new HashMap<String, Object>();
		condition.put("tableNamePre", tableNamePre);
		List<Map<String,Object>> list = commonMapper.selectTableNames(condition);
		for(Map<String,Object> map : list){
			String tableName = map.get("tableName").toString();
			String sql = " drop table "+tableName;
			commonMapper.excuteDDL(sql);
		}
	}

	@Override
	public Map<String, Object> findOne(Map<String, Object> map)
			throws ServiceException {
		return commonMapper.selectOne(map);
	}

	@Override
	public int updatePostVotes(Map<String, Object> map) throws ServiceException {
		try {
			return commonMapper.updatePostVotes(map);
		} catch (Exception e) {
			throw new ServiceException("更新内容的投票值出错");
		}
	}

	@Override
	public int updatePostViews(Map<String, Object> map) throws ServiceException {
		try {
			return commonMapper.updatePostViews(map);
		} catch (Exception e) {
			throw new ServiceException("更新内容的查看次数出错");
		}
	}
}
